export default {
  namespaced: true,

  // 数据
  state: () => ({
    address: JSON.parse(uni.getStorageSync("address") || "{}"),
    token: uni.getStorageSync("token") || "",
    userinfo: JSON.parse(uni.getStorageSync("userinfo") || "{}"),
    redirectInfo: null
  }),

  // 方法
  mutations: {
    // 更新收货地址
    updateAddress(state, address) {
      state.address = address
      this.commit("m_user/saveStorage", "address")
    },

    // 本地存储
    saveStorage(state, type) {
      uni.setStorageSync(type, JSON.stringify(state[type]))
    },

    // 更新用户信息
    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo;
      this.commit("m_user/saveStorage", "userinfo")
    },

    // 更新 token
    updateToken(state, token) {
      state.token = token;
      uni.setStorageSync("token", token)
    },

    // 更新重定向信息
    updateRedirectInfo(state, info) {
      state.redirectInfo = info;
    }
  },

  // 计算属性
  getters: {
    addressStr(state) {
      if (!state.address.provinceName) return ""
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}